package cn.kgc.qnbbs.controller;

import cn.kgc.qnbbs.Service.DetailService;
import cn.kgc.qnbbs.pojo.Detail;
import cn.kgc.qnbbs.pojo.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class DetailController {
    @Resource
    DetailService detailService;

    @RequestMapping("/selectDetail")
    public ResultEntity<PageInfo<Detail>> selectDetail(@RequestParam(value = "sortId", required = false, defaultValue = "0") Integer sortId,
                                                       @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        PageInfo<Detail> pageInfo = detailService.select(pageNum, pageSize, sortId);
        return ResultEntity.successWithoutData(pageInfo);
    }

    @RequestMapping("/saveDetail")
    public ResultEntity<String> saveDetail(@RequestBody Detail detail) {
        try {
            Integer integer=0;
            if(detail.getId()==null){
                integer=detailService.add(detail);
            }else {
                integer=detailService.update(detail);
            }
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }
    }
    @RequestMapping("/getDetailById")
    public ResultEntity<Detail> getDetailById(Integer id){
        Detail detail=detailService.selectById(id);
        return ResultEntity.successWithoutData(detail);
    }
    @RequestMapping("/delDetailById")
    public ResultEntity<String> delDetailById(Integer id){
        try {

        Integer integer=detailService.delete(id);
            return ResultEntity.successWithoutData(integer+"");

        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }
    }
}
